package com.healthbox.appointmentservice.service.impl;

import com.healthbox.appointmentservice.DVO.AppointmentLimitDVO;
import com.healthbox.appointmentservice.entity.AppointmentLimit;
import com.healthbox.appointmentservice.mapper.AppointmentLimitMapper;
import com.healthbox.appointmentservice.service.AppointmentLimitQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author pengpeng
 * @date 2022/5/22
 */
@Service
public class AppointmentLimitQueryImpl implements AppointmentLimitQueryService {
    @Autowired
    private AppointmentLimitMapper limitRepo;
    @Override
    public AppointmentLimitDVO findCurrentInfoById(Long appointmentLimitId) {
        AppointmentLimit appointmentLimit = limitRepo.findOneById(appointmentLimitId);
        Integer currentNumber = limitRepo.numberOfAppointment(appointmentLimitId);
        return AppointmentLimitDVO.fromEntity(appointmentLimit, currentNumber);
    }

    @Override
    public List<AppointmentLimitDVO> findAppointmentLimits(LocalDate start, LocalDate end, Long hospitalId) {
        List<AppointmentLimit> appointmentLimits = limitRepo.findByDateAndHospitalId(start, end, hospitalId, null, null);
        List<AppointmentLimitDVO> vos = new ArrayList<>();
        for (AppointmentLimit appointmentLimit : appointmentLimits) {
            Integer currentNumber = limitRepo.numberOfAppointment(appointmentLimit.getALId());
            vos.add(AppointmentLimitDVO.fromEntity(appointmentLimit, currentNumber));
        }
        return vos;
    }
}
